/*
  将下面异步代码使用 Promise 的方法改进
  尽量用看上去像同步代码的方式
  setTimeout(function () {
    var a = 'hello'
    setTimeout(function () {
      var b = 'lagou'
      setTimeout(function () {
        var c = 'I ♥ U'
        console.log(a + b +c)
      }, 10)
    }, 10)
  }, 10)
*/

const timeoutPromise = value => new Promise(resolve => setTimeout(() => resolve(value), 10));


// 方案一
const promise = timeoutPromise('hello')
  .then(value => timeoutPromise(`${value} lagou`))
  .then(value => timeoutPromise(`${value} I ♥ U`))
  .then(value => console.log(value));

  //方案二
(async function() {
  const a = await timeoutPromise('hello');
  const b = await timeoutPromise('lagou');
  const c = await timeoutPromise('I ♥ U');
  console.log(`${a} ${b} ${c}`)
})();

//方案三
const g = (function* () {
  const a = yield timeoutPromise('hello');
  const b = yield timeoutPromise('lagou');
  const c = yield timeoutPromise('I ♥ U');
  console.log(`${a} ${b} ${c}`);
})();

(function handleResult(value) {
  const next = g.next(value);
  if (next.done) return;
  next.value.then(value => handleResult(value));
})();
